import { ref, onMounted, onBeforeUnmount } from 'vue'
import axios from 'axios'

export default function useTask() {
  let timer = null
  const task_process = ref({})

  const getTask = async () => {
    try {
      const res = await axios.request({
        url: '/api/task',
        method: 'get',
        params,
      })
      task_process.value = res.data
      if (task_process.value.has_process) {
        timer = setTimeout(() => {
          getTask()
        }, 3000)
      }
    } catch (err) {
      console.log(err.message)
    }
  }

  onMounted(() => {
    getTask()
  })

  onBeforeUnmount(() => {
    clearTimeout(timer)
  })

  return {
    task_process,
    getTask,
  }
}